/* $Revision: $ $Date: $
 * 
 * Copyright (C) 2009 Robert Stephan
 * 
 * This file is part of *** CaCaO - Card Catalogs Online ***
 *
 * CaCaO is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * CaCao is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package cacao.client.ui;

import java.util.List;

import cacao.client.CaCaO;
import cacao.client.data.CaCaOHelper;
import cacao.client.data.CatalogObject;
import cacao.client.data.SearchCommand;
import cacao.client.data.SearchResultObject;

import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.MenuBar;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.VerticalPanel;


public class LeftMenuCmp extends Composite implements CaCaOComposite{
	private CaCaO application;
	private HTML lblDescription, lblFooter, lblResultList;
	DockLayoutPanel panel;			

	private MenuBar menuResultList;

	public LeftMenuCmp(CaCaO app){
		application=app;

		panel = new DockLayoutPanel(Unit.PX);
		panel.setStylePrimaryName("panel-leftmenu");
		lblDescription = new HTML("", true);
		lblDescription.setStylePrimaryName("lbl-leftmenu-description");
		panel.addNorth(lblDescription, 60);

		lblFooter = new HTML(application.getMessages().footerHTML());
		lblFooter.setStylePrimaryName("lbl-leftmenu-footer"); 
		panel.addSouth(lblFooter, 80);

		VerticalPanel vpCenter = new VerticalPanel();
		lblResultList = new HTML("");
		vpCenter.add(lblResultList);
		vpCenter.setStylePrimaryName("vp-leftmenu-resultList");
		vpCenter.setWidth("100%");
		vpCenter.setHeight("100%");
		menuResultList = new MenuBar(true);
		vpCenter.add(menuResultList);
		vpCenter.getElement().setAttribute("style", "overflow:scroll");

		//add to center
		panel.add(vpCenter);
		initWidget(panel);
	}

	public void updateUI(){
		int selIndex = application.getCatalogSelectionIndex();
		List<CatalogObject> catalogs = application.getCatalogs();
		if(selIndex>=0 && selIndex<catalogs.size()){
			String html = CaCaOHelper.getCatalogSpecificString(application.getMessages().catalogInfoHTML_JSON(), catalogs.get(selIndex).getCode());
			lblDescription.setHTML(html);
		}

		//SearchResultList
		SearchResultObject sro = application.getSearchResult();
		menuResultList.clearItems();

		if(sro.getResult()!=null){
			lblResultList.setHTML(application.getMessages().resultlist());
			for(String s: sro.getResultList()){
				if(s.equals(sro.getResult())){
					menuResultList.addSeparator();
					MenuItem mi = new MenuItem("<b>"+s+"</b>", true, new SearchCommand(s, application));
					mi.setHeight("10px");
					menuResultList.addItem(mi);
					menuResultList.addSeparator();
				}
				else{
					MenuItem mi = new MenuItem("<span style=''>"+s+"</span>", true, new SearchCommand(s, application));
					menuResultList.addItem(mi);
				}
			}
		}
		else{
			lblResultList.setHTML(application.getMessages().resultlist());
			menuResultList.clearItems();
			for(String s: sro.getResultList()){
				MenuItem mi = new MenuItem("<span style=''>"+s+"</span>", true, new SearchCommand(s, application));
				menuResultList.addItem(mi);
			}
		}
	}
}
